/*
	Copyright (c) 2008, Grant Jones <grant@podtronics.com>.
	All rights reserved.

	Redistribution and use in source and binary forms, with or
	without modification, are permitted provided that the following
	conditions are met:

		Redistributions of source code must retain the above copyright
		notice, this list of conditions and the following disclaimer.
		
		Redistributions in binary form must reproduce the above copyright
		notice, this list of conditions and the following disclaimer in
		the documentation and/or other materials provided with the
		distribution.
		
		Neither the name of the gxjones.com nor the names of its
		contributors may be used to endorse or promote products derived
		from this software without specific prior written permission.
	
	THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
	"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
	LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
	FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
	COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
	INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
	(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
	SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
	HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
	STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
	OF THE POSSIBILITY OF SUCH DAMAGE.
	
	http://www.opensource.org/licenses/bsd-license.php
*/

/*
 *  edb_msg_parser.h
 *  scatterd
 *
 *  Created by Grant Jones on 2/24/08.
 *
 */

#ifndef EDB_MSG_PARSER_H
#define EDB_MSG_PARSER_H

#include "parse.h"

#include "edb_net_shell.h"



//#include "edb_msg_expr.h"


/*!
	@function edb_msg_parser
	@abstract Interprets the preamble of a request
	@discussion 
	@param pcs The current program context
	@param pool mempool to inherit, may be NULL (in which case a new mempool is created)
	@param preamble The request to parse
	@param item_count The number of associated items with the request
	@param items An array of items (of size item_count)
	@result A new map containing the parser result (an map containing ordered edb_net_item_t values)
*/
cmapStruct *edb_msg_parser( ProgramContextStruct *pcs, mempool_t *pool, const char *preamble, size_t item_count, edb_net_item_t *items );



typedef struct 
{
	int parseError;
	char *parseErrorStr;
	ProgramContextStruct *pcs;
	
	size_t current_in_item;
	size_t in_item_count;
	edb_net_item_t *in_items;
	
	cmapStruct *out_items;
} edbParseContext;




typedef struct
{
	Token key;
	Token value;
} AttrValue;

typedef cmapStruct AttrList;

typedef struct Expr Expr;

struct Expr {
	unsigned char op;
	Expr *pLeft, *pRight;
	Token token;
	Token span;
};

/* includes for the parser: */
#include "edb_msg_utils.h"
#include "edb_msg_tokenize.h"
#include "edb_msg_expr.h"

/* protos for parser: */
void edbBeginParse( edbParseContext *ctx, int explain );
void edbFinishCoding( edbParseContext *ctx );
void edbDeleteFrom( edbParseContext *ctx, Token *from_name, Expr *where, AttrList *what );
void edbCreateDomain( edbParseContext *ctx, Token *name, AttrList *default_attrs );
void edbDropDomain( edbParseContext *ctx, Token *name );
void edbSelect(edbParseContext *ctx, AttrList *selattrlist, Token *from_name, Expr *where_expr, void *groupby_arg, void *having_arg, AttrList *orderby_arg, AttrList *limit_arg);
void edbInsert( edbParseContext *ctx, Token *name, AttrList *insert_values, Expr *where_expr );
/* interface protos for parser: */
void edbParser( void *, int, Token, edbParseContext * );
void *edbParserAlloc(void *(*mallocProc)(size_t));
void edbParserFree( void *p, void (*freeProc)(void*) );

#endif


